www.gusucode.com > Simulink for Circuit Analysis 工具箱matlab源码程序 > Simulink for Circuit Analysis/Compute_S_Parameters.m
function Compute_S_Parameters(model,varargin) % model = model name, use gcs % Dick Benson, October 2018 Fstart = str2num(get_param([model,'/Fstart'],'Value')); % the names of these Fstop = str2num(get_param([model,'/Fstop'],'Value')); % constant blocks Npts = str2num(get_param([model,'/Number_of_Points'],'Value')); % must match the model % Get the analysis I/Os from the model io = getlinio(model); % Note the ordering of the ports is carefully forced in the model's Directional Bridges. % If the Directional Bridge blocks are edited, all bets are off. % The assumed ordering for 1 port measurements is: % P1_Drive = 1 P1_Response = 2 % The assumed ordering for 2 port measurement is: % P1_Drive=1 P2_Drive= 2 % P1_Response=3 P2_Response=4 % For a 3 port it would be: % P1_Drive=1 P2_Drive= 2 P3_Drive=3 % P1_Response=4 P2_Response=5 P3_Response=6 % For a 4 port: % P1_Drive=1 P2_Drive= 2 P3_Drive=3 P4_Drive=4 % P1_Response=5 P2_Response=6 P3_Response=7 P4_Response=8 tic % start the timer if isempty(varargin) % There was no time specified to set the operating point. op = operpoint(model); % The IC should all be zero for passive linear model. % Linearize the model, but it should already be linear! sys = linearize(model,io,op); else % Run the model for a specified amount of time to allow startup % transients to die and establish an operating point. % Note that this has not been extensivly tested. sys = linearize(model,io,varargin{1}); end; dF = (Fstop-Fstart)/(Npts-1); Fvec = Fstart:dF:Fstop; % Attempt to optimize using the "prescale()" function. focus={Fvec(2)*2*pi,Fvec(end)*2*pi}; scaledsys = prescale(sys,focus); % ASSUME an equal number of inputs and output ports. % length(io) = 2, 1x1 % length(io) = 4, 2x2 % length(io) = 6, 3x3 % length(io) = 8, 4x4 N=length(io)/2; % Create an "rf object" rf_obj = rfdata.data; rf_obj.Z0 = 50; % this is the assumed Zo of the bridges rf_obj.Freq = Fvec; rf_obj.S_Parameters = complex(zeros(N,N,Npts)); rf_obj.S_Parameters = freqresp(scaledsys,2*pi*Fvec); trun=toc; State = { ['The S parameters were generated from Simulink model: ',model,'.slx/.mdl'];... ['This computation took ',num2str(trun,2),' seconds.']}; Notes = {'You can optionally place your own notes in this edit box and re-save the sNp.'}; Path=''; FileName=[model,'_Sparameters.s',num2str(N,1),'p']; spar_write(Path,FileName,rf_obj,Notes,State); % Save to an sNp file. Path=''; Spar_Plot(Path,FileName); end